package org.nuonuo.sdk.client;


import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.nuonuo.sdk.exception.NException;

import java.io.IOException;
import java.util.*;

/**
 * @Version 1.0
 * @Author az
 * @Date 2024-03-28 11:03:58
 * @ClassName ModuleAuth
 * @Description
 */

final class ModuleAuth {
    public static final String TOKEN_URL = "https://open.nuonuo.com/accessToken";

    ModuleAuth() {
    }

    static String getMerchantToken(String appKey, String appSecret, Map<String, String> header, String... url) {
        Map<String, String> param = new HashMap();
        param.put("client_id", appKey);
        param.put("client_secret", appSecret);
        param.put("grant_type", "client_credentials");
        return doPost(url.length == 1 ? url[0] : TOKEN_URL, param, header);
    }

    static String getIsvToken(String code, String taxnum, String appKey, String appSecret, String redirectUri, Map<String, String> header, String... url) {
        Map<String, String> param = new HashMap();
        param.put("client_id", appKey);
        param.put("client_secret", appSecret);
        param.put("grant_type", "authorization_code");
        param.put("redirect_uri", redirectUri);
        param.put("code", code);
        param.put("taxNum", taxnum);
        return doPost(url.length == 1 ? url[0] : TOKEN_URL, param, header);
    }

    static String refreshIsvToken(String refreshToken, String userId, String appSecret, Map<String, String> header, String... url) {
        Map<String, String> param = new HashMap();
        param.put("refresh_token", refreshToken);
        param.put("client_id", userId);
        param.put("client_secret", appSecret);
        param.put("grant_type", "refresh_token");
        return doPost(url.length == 1 ? url[0] : TOKEN_URL, param, header);
    }

    private static String doPost(String url, Map<String, String> param, Map<String, String> header) {
        String result = null;
        HttpHost proxy = null;
        if (header != null && header.containsKey("proxyHost") && header.containsKey("proxyPort")) {
            proxy = new HttpHost(header.get("proxyHost"), Integer.parseInt(header.get("proxyPort")), "http");
        }

        HttpPost httpPost = new HttpPost(url);
        HttpConnPool.setRequestConfig(httpPost, proxy, new int[0]);
        List<NameValuePair> list = new ArrayList();
        if (param != null && !param.isEmpty()) {
            Iterator i$ = param.entrySet().iterator();

            while(i$.hasNext()) {
                Map.Entry<String, String> elem = (Map.Entry)i$.next();
                list.add(new BasicNameValuePair(elem.getKey(), elem.getValue()));
            }
        }

        httpPost.setEntity(new UrlEncodedFormEntity(list, Consts.UTF_8));

        try {
            CloseableHttpResponse response = HttpClients.createDefault().execute(httpPost, HttpClientContext.create());
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                result = EntityUtils.toString(entity, Consts.UTF_8);
            }

            response.close();
            return result;
        } catch (IOException var9) {
            throw new NException("接口调用异常【url=" + url + "】", var9);
        }
    }
}

